using System;
using System.Data;
using PointOfSale.Global;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace PointOfSale.Class
{
	public class clsSaleDetail
	{
		private int saleDetailId;
		private string soHoaDonCon;
		private int saleId;
		private int itemId;
		private double qty;
		private double rate;
		private int unitId;
		private double amount;

		#region Properties.
		public int SaleDetailId
		{
			get
			{
				return saleDetailId;
			}
			set
			{
				saleDetailId = value;
			}
		}
		public string SoHoaDonCon
		{
			get
			{
				return soHoaDonCon;
			}
			set
			{
				soHoaDonCon = value;
			}
		}
		public int SaleId
		{
			get
			{
				return saleId;
			}
			set
			{
				saleId = value;
			}
		}
		public int ItemId
		{
			get
			{
				return itemId;
			}
			set
			{
				itemId = value;
			}
		}
		public double Quantity
		{
			get
			{
				return qty;
			}
			set
			{
				qty = value;
			}
		}
		public double Rate
		{
			get
			{
				return rate;
			}
			set
			{
				rate = value;
			}
		}
		public int UnitId
		{
			get
			{
				return unitId;
			}
			set
			{
				unitId = value;
			}
		}
		public double Amount
		{
			get
			{
				return amount;
			}
			set
			{
				amount = value;
			}
		}
		#endregion

		public clsSaleDetail()
		{
			
		}
		public void Add()
		{
			if(soHoaDonCon.Length == 0)
			{
				soHoaDonCon = "-1";
			}
			String strInsert = "INSERT into tblSaleDetail " + 
				"(SaleId, ItemId, Qty, Rate, UnitId, Amount, soHoaDonCon) " + 
				"VALUES ("			+ "'" +
				saleId				+ "', '" +
				itemId				+ "', " +
				qty					+ ", " +
				rate				+ ", '" +
				unitId				+ "', " +
				amount				+ ", "  +
				soHoaDonCon			+ ") ";
			clsDatabase.ExecuteSQL(strInsert, clsGlobal.g_DataConn);
		}
		public static DataTable GetBillDetail(int saleId)
		{
			String strSelect = "select a.soHoaDonCon, a.saleId, a.itemId, a.qty, a.rate, a.unitId, a.amount, b.itemName, c.unitName "
							+ " from tblSaleDetail a inner join tblItem b on a.itemId = b.itemId "
							+ " inner join tblUnit c on a.unitId = c.unitId "
							+ " where a.saleId = " + saleId;
			DataTable dt = new DataTable();
			dt = clsDatabase.GetDataToTable(strSelect, clsGlobal.g_DataConn);
			return dt;
		}
		public static void Delete(int saleId)
		{
			String strDelete = "DELETE tblSaleDetail WHERE SaleId = " + saleId;
			clsDatabase.ExecuteSQL(strDelete, clsGlobal.g_DataConn);
		}
		public static SqlDataReader getSaleBillInformation(string si)
		{
			string selectCommand = "select a.itemID, a.qty, a.rate, a.unitID, a.saleID, a.amount, b.itemName, b.itemCode, c.unitName "
				+ " from tblSaleDetail a inner join tblItem b on a.itemID = b.itemID "
				+ " inner join tblUnit c on a.unitID = c.unitID "
				+ " where a.saleID = " + si;
			SqlDataReader dr = clsDatabase.GetDataToDataReader(selectCommand, clsGlobal.g_DataConn);
			return dr;
		}
	}
}
